package com.hivekion.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hivekion.system.domain.SysMember;
import com.hivekion.system.domain.dto.member.SysMemberModifyDto;
import com.hivekion.system.domain.dto.member.SysMemberPageQuery;
import com.hivekion.system.domain.dto.member.SysMemberSaveDto;
import com.hivekion.system.domain.vo.member.SysMemberPageVo;
import com.hivekion.system.domain.vo.member.SysMemberVo;
import com.hivekion.system.mapper.SysMemberMapper;
import com.hivekion.system.service.ISysMemberService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ISysMemberServiceImpl extends ServiceImpl<SysMemberMapper, SysMember> implements ISysMemberService {

    @Override
    public Page<SysMemberPageVo> getPage(SysMemberPageQuery query) {

        LambdaQueryWrapper<SysMember> wrapper = Wrappers.lambdaQuery();

        if (StrUtil.isNotEmpty(query.getName())) {
            wrapper.like(SysMember::getName, query.getName());
        }

        if (query.getType() != null) {
            wrapper.like(SysMember::getType, query.getType().toString());
        }

        Page<SysMember> iPage = new Page<>();
        iPage.setCurrent(query.getPageNum());
        iPage.setSize(query.getPageSize());

        Page<SysMember> page = this.page(iPage, wrapper);

        Page<SysMemberPageVo> iPageVo = new Page<>();

        List<SysMemberPageVo> list = new ArrayList<>();

        for (SysMember record : page.getRecords()) {
            SysMemberPageVo vo = new SysMemberPageVo();
            BeanUtil.copyProperties(record, vo);
            vo.setTypeLabel(getTypeLabels(vo.getType()));
            vo.setSexName(vo.getSex() == 0 ? "女" : "男");
            list.add(vo);
        }

        iPageVo.setRecords(list);
        iPageVo.setTotal(page.getTotal());
        iPageVo.setCurrent(page.getCurrent());
        iPageVo.setSize(page.getSize());
        iPageVo.setTotal(page.getTotal());

        return iPageVo;
    }

    @Override
    public boolean saveDto(SysMemberSaveDto dto) {
        SysMember member = new SysMember();
        BeanUtil.copyProperties(dto, member);
        return this.save(member);
    }

    @Override
    public SysMemberVo getDetail(String id) {

        SysMember member = this.baseMapper.selectById(id);
        SysMemberVo vo = new SysMemberVo();
        BeanUtil.copyProperties(member, vo);
        return vo;
    }

    @Override
    public boolean modifyDto(SysMemberModifyDto dto) {
        SysMember member = new SysMember();
        BeanUtil.copyProperties(dto, member);
        return  this.updateById(member);
    }

    private String getTypeLabels(String type) {
        List<String> list = Arrays.asList(type.split(","));
        List<String> temp = new ArrayList<>();
        for (String s : list) {
            temp.add(getTypeLabel(s));
        }
        return String.join(",", temp);
    }

    private String getTypeLabel(String type) {
        String result = "";
        switch (type) {
            case "1":
                result = "Vue开发";
                break;
            case "2":
                result = "Java开发";
                break;
            case "3":
                result = "Wpf开发";
                break;
            case "4":
                result = "UniApp开发";
                break;
        }
        return result;
    }
}
